package lab.gene.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import lab.gene.pojo.PrimerSequence;

@SuppressWarnings("deprecation")
public class PrimerSequenceDAO {
	
	private static SessionFactory factory = new AnnotationConfiguration().configure().addAnnotatedClass(PrimerSequence.class).addPackage("com.xyz").buildSessionFactory();
	
	@SuppressWarnings("unchecked")
	public List<PrimerSequence> getAllPrimerSequencesByGeneName(String geneName) {
		List<PrimerSequence> primerSequences = null;
		try {
			Session session = factory.openSession();
	        try {
	        	String hql = "FROM PrimerSequence P where P.gene.geneName = '"+geneName+"'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	List<PrimerSequence> results = query.list();
	        	System.out.println("# primer sequence:"+results.size());
	        	if (results.size() > 0) {
	        		primerSequences = results;
	        		System.out.println("sequences found!"); 
	        	}
	        	else {
	        		System.out.println("result.size = "+ results.size());
	        		System.out.println("empty sequences");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return primerSequences;
	}
	
	@SuppressWarnings("unchecked")
	public PrimerSequence getPrimerSequenceByName(String gene, String primer) {
		//List<PrimerSequence> results = null;
		PrimerSequence pSeq = null;
		try {
			System.out.println("Primer DAO");
			Session session = factory.openSession();
	        try {
	        	String hql = "FROM PrimerSequence P where P.gene.geneName = '"+gene+"' and P.primer = '" + primer +"'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	List<PrimerSequence> results = query.list();
	        	//System.out.println("user:"+results.size());
	        	if (results.size() > 0) {
	        		pSeq = results.get(0);
	        		System.out.println("Sequence found!"); 
	        	}
	        	else {
	        		System.out.println("no sequence found, going to add");
	        	}
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
	        finally {
	            session.close(); 
	        }
	   } 
		catch (Throwable ex) { 
			System.err.println("Failed to create sessionFactory object." + ex);
	        throw new ExceptionInInitializerError(ex);
	   }
		return pSeq;
	}
	
	public void addPrimerSequence(PrimerSequence pSeq) {
		try {
	         Session session = factory.openSession();
	         Transaction tx = null;
	         try {
	        	 tx = session.beginTransaction();
	        	 session.save(pSeq); 
	        	 tx.commit();
	        	 } 
	         catch (HibernateException e) {
	        	 if (tx!=null) 
	        		 tx.rollback();
	        	 e.printStackTrace(); 
	         } 
	         finally {
	            session.close(); 
	         }
	      } catch (Throwable ex) { 
	         System.err.println("Failed to create sessionFactory object." + ex);
	         throw new ExceptionInInitializerError(ex); 
	      }
		
	}
	
	public void updatePrimerSequence(int id, String primer, String well, String plate) {
		Session session = factory.openSession();
	    Transaction tx = null;
	    try{
	         tx = session.beginTransaction();
	         PrimerSequence pSeq = (PrimerSequence)session.get(PrimerSequence.class, id); 
	         pSeq.setPlate(plate);
	         pSeq.setWell(well);
			 session.update(pSeq); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	     }
	}

public void deletePrimerSequencebyId(Integer id) {
		System.out.println("---DB");
		Session session = factory.openSession();
		Transaction tx = null;
		try {
		   try {
			   tx = session.beginTransaction();
	        	String hql = "DELETE FROM PrimerSequence P "  + "WHERE P.id = '" + String.valueOf(id) + "'";
	        	System.out.println(hql);
	        	Query query = session.createQuery(hql);
	        	query.executeUpdate();
	        	tx.commit();
	        }
	        catch (HibernateException e) {
	        	e.printStackTrace();
	        } 
		}
		catch (Exception e) {
		   if (tx!=null) tx.rollback();
		   e.printStackTrace(); 
		}finally {
		   session.close();
		}
	}

}
